// PROGRAMA : SCRPE003
// BIBLIOTECA : SISP
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : * DATA: 02/08/2002
// OBJETIVO :
// OBJETIVO : EMITE QUADRO ESTATISTICO. (BATCH)
// ID. CIVIL

package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.io.FileNotFoundException;
import java.text.ParseException;

import net.sf.jasperreports.engine.JRException;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Scrpt001Report;
import br.com.vti.sisp_sicrim.antigo.model.SicEstatistica;
import br.com.vti.sisp_sicrim.antigo.model.Sisldat8;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Estpt131 OBJETIVO : OBJETIVO : EMITE QUADRO ESTATISTICO. (BATCH) ID. CIVIL *
 * 
 * @author Incode - Data: mai/2014
 * 
 */

@Resource
@Path("/antigo/estpt131")
public class Estpt131 {
	// Views utilizadas
	SicEstatistica vwCadest = new SicEstatistica();
	// Telas/relatorios utilizados
	// Estpt131A estpt131A;
	Scrpt001Report report;
	// Includes utilizados
	Sisldat8 sisldat8 = new Sisldat8();
	// Areas globais / session
	Sisgsisp sisgsisp; // Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();
	
	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	private int[] tot0;
	private long[] totp;
	private int k;
	private String motida;
	private String motid2;
	private int wnumpos;

	public Estpt131(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	private InputStreamDownload main(String[] args) throws Exception {
		// estpt131A = new Estpt131A(true, sessao);
		// scrpt001 = new Scrpt001(true, sessao);
		report = new Scrpt001Report();
		String datini = args[0];
		String datfim = args[1];
		String matric = args[2];
		String senha = args[3];
		report.setDatini(Integer.parseInt(datini));
		report.setDatfim(Integer.parseInt(datfim));

		Estpb131 estpb131 = new Estpb131(sessao);
		estpb131.executar(Integer.valueOf(datini), Integer.valueOf(datfim), wnumpos, Integer.valueOf(matric), senha);
		// //DEFINE PRINTER(BUC = 1)
		for (int i1 = 1; i1 <= 1; i1++) {
			tot0[i1] = 0;
		}
		report.setDatfim(Integer.parseInt(datfim));
		motida = UtilBean.moveSubstring(motida, 0, 3, "A", "IDC");
		motida = UtilBean.moveSubstring(motida, 3, 2, "N", 99);
		// ASSIGN MAT = 34165
		// DATINI:=01012002
		// DATFIM:=09092002

		// DISPLAY DATINI DATFIM

		motid2 = motida;
		sisldat8.dtini = report.getDatini();
		sisldat8.dtini = (int) funcoes.sissd002(sisldat8.dtini);
		sisldat8.dtfim = report.getDatfim();
		sisldat8.dtfim = (int) funcoes.sissd002(sisldat8.dtfim);

		while (vwCadest.leituraMotida()) {
			if (vwCadest.quebraDinestdatatu()) {

				// estpt131A.enviarReceberTela(false, result);
				if (vwCadest.getDataAtualiz() >= sisldat8.dtini && vwCadest.getDataAtualiz() <= sisldat8.dtfim && vwCadest.getQuantTrans(7) > 0 && vwCadest.getNumeroPosto() == wnumpos) {
					k += 1;
					if (k == 1) {
						report.setMnome(k, " ");
					} else {
						if (k > 50) {
							k = 1;
						}
						report.setMnome(k, "TOTAL..............:");
					}

					report.setMtotal(k, (int) totp[1]);
					totp[1] = 0;
				}
			}

			// IF DIAEST-CODMOD = 'IDC' && DINEST-CODTIP = 99 &&
			// DINEST-QUANTI(7)>0
			// && DINEST-DATATU = DTINI THRU DTFIM

			if (vwCadest.getDataAtualiz() >= sisldat8.dtini && vwCadest.getDataAtualiz() <= sisldat8.dtfim && vwCadest.getQuantTrans(7) > 0 && vwCadest.getNumeroPosto() == wnumpos) {
				motida = UtilBean.moveSubstring(motida, 5, 8, "A", funcoes.sissd002(vwCadest.getDataAtualiz()));
				k += 1;
				if (k > 50) {
					impr();
					k = 1;
				}

				report.setMmat(k, vwCadest.getMatriculaFunc());
				report.setMdata(k, Integer.parseInt(UtilBean.subStr(motida, 5, 8)));
				report.setMhora(k, vwCadest.getHoraem());
				report.setMnome(k, vwCadest.getNomeId());
				report.setMregger(k, vwCadest.getNumeroRg());
				report.setMposto(k, vwCadest.getNumeroPosto());
				report.setMtotal(k, (int) (report.getMtotal(k) + vwCadest.getQuantTrans(7)));
				totp[1] = totp[1] + vwCadest.getQuantTrans(7);
				report.setTot0(1, (int) (report.getTot0(1) + vwCadest.getQuantTrans(7)));
			}

		}
		vwCadest.fecha();
		if (vwCadest.registrosProcessados() == 0) {
			UtilBean.mensagemTexto("INTERVALO INEXISTENTE");
		}

		return impr();
	}

	private InputStreamDownload impr() throws ParseException, JRException, FileNotFoundException {
		// RELATORIO
		report.gravaRelatorio(1);
		return report.imprimir();
	}
}
